var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.messagesendlist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.data);
        });
    },
    getDataList: function(data) {
        var _this = this;
        if(data && data.length > 0) {
            var i = 0; len = data.length, html = '';
            for (i;i<len;i++) {
                html+='<tr>'+
                            '<td class="text-c">'+data[i].typeName+'</td>'+
                            '<td class="text-c">'+
                                '<span class="mr-15">'+
                                    '<div class="goods-checkbox mr-5">'+
                                        '<div class="check-box">';
                                        if (data[i].isApp==1) {
                                            html+='<input type="checkbox" name="send" isApp="1" sendId="'+data[i].sendId+'" checked="checked">';
                                        }else{
                                            html+='<input type="checkbox" name="send" isApp="1" sendId="'+data[i].sendId+'" >';
                                        };
                                html+='</div>'+
                                    '</div>是否发送'+
                                '</span>'+
                                '<a class="c-primary edit" sendId="'+data[i].sendId+'">编辑内容</a>'+
                            '</td>'+
                            '<td class="text-c">'+
                                '<div class="goods-checkbox mr-5">'+
                                    '<div class="check-box">';
                                        if (data[i].isDefault==1) {
                                            html+='<input type="checkbox" name="send" isApp="0" sendId="'+data[i].sendId+'" checked="checked">';
                                        }else{
                                            html+='<input type="checkbox" name="send" isApp="0" sendId="'+data[i].sendId+'" >';
                                        };
                                html+='</div>'+
                                '</div>'+
                                '<a class="c-primary edit" sendId="'+data[i].sendId+'">编辑内容</a>'+
                            '</td>'+
                        '</tr>';
            }
            $('#lists').html(html);
            _this.getDataEvent()
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="3" >暂无数据</td>' +
                '</tr>';
            $('#lists').html(html);
        }
    },
    getDataEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.edit').on('click', function() {
            Pub.setSession('sendId', $(this).attr('sendId'));
            window.location.href = Pub.getHtmlUrl('./message-remind-edit.html');
        });
        $('input[name="send"]').on('ifChanged', function() {
            if ($(this).is(':checked')) {
                var isSend=1;
            } else {
                var isSend=0;
            }
            var isApp=$(this).attr('isApp');
            var sendId=$(this).attr('sendId');
            _this.updataSend(sendId,isApp,isSend);
        });
    },
    updataSend:function(sendId,isApp,isSend){
        var _this = this;
        if (isApp==1) {
            var paramsObj = {
                passport: _this.params.passport,
                sendId:sendId,
                isApp:isSend
            };
            console.log(paramsObj);
            Api.messagesendmodifyapp(paramsObj, function(data){
                console.log(data);
                _this.getData();
            });
        }else{
            var paramsObj = {
                passport: _this.params.passport,
                sendId:sendId,
                isDefault:isSend
            };
            console.log(paramsObj);
            Api.messagesendmodifydefault(paramsObj, function(data){
                console.log(data);
                _this.getData();
            });
        };

    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.otherEvent();


    }
};
var page = new Page();